 --EXEC SP_AGED_BACKLOG_REPORT_PROC '0','WITHHOLD','REQUESTED','0'
 
 ALTER PROC SP_AGED_BACKLOG_REPORT_PROC
 @BASEPRODUCTID	VARCHAR(10),	-- Base Product Id
 @HOLDTYPE	VARCHAR(20),	-- Hold Type
 @CATEGORY	VARCHAR(10),	-- Requested Date or Schedule Date  
 @DEFAULT_PROMPT VARCHAR(20)
 AS
 BEGIN
 	DECLARE @PRODUCT_ID INT
 	DECLARE @LINEITEM_ID INT
 	DECLARE @COOL_QTY INT
 	DECLARE @COOL_AMOUNT FLOAT
 	DECLARE @COOL_REQUESTED_DATE DATETIME
 	DECLARE @COOL_SCHEDULE_DATE DATETIME
 	DECLARE @SO_BOOKING_DATE DATETIME
	DECLARE @CATEGORY_DATE DATETIME
 	DECLARE @PARTIAL_SHIPPED_QTY INT
 
 	DECLARE @SYSTEM_WEEK INT
	DECLARE @LINEITEM_WEEK INT
 	DECLARE @LINEITEM_QTR  INT
 	DECLARE @LINEITEM_YEAR  INT

 	DECLARE @CURRENT_QTR  INT
 	DECLARE @CURRENT_YEAR INT
 	DECLARE @CURRENT_QTR_START DATETIME
	DECLARE @CURRENT_QTR_END DATETIME
 
 	DECLARE @BOOKING_DATE_WEEK INT
 	DECLARE @BOOKING_DATE_QTR INT
 	DECLARE @BOOKING_DATE_YEAR INT
	DECLARE @IS_SHIPPED_QTY_REDUCE BIT

 	DECLARE @BOOKINGWEEK VARCHAR(50)
 
 	SET @CURRENT_QTR = DATEPART ( QQ, GETDATE() ) 
 	SET @CURRENT_YEAR = DATEPART ( YYYY, GETDATE() )
 
 	DECLARE @RESULTS_TABLE TABLE ( BOOKING_WEEK VARCHAR(10), DELIQUENT_QTY INT, DELIQUENT_AMOUNT FLOAT, WEEK1_QTY INT, WEEK1_AMOUNT FLOAT,
 				       WEEK2_QTY INT, WEEK2_AMOUNT FLOAT, WEEK3_QTY INT, WEEK3_AMOUNT FLOAT,
 				       WEEK4_QTY INT, WEEK4_AMOUNT FLOAT, WEEK5_QTY INT, WEEK5_AMOUNT FLOAT,
 				       WEEK6_QTY INT, WEEK6_AMOUNT FLOAT, WEEK7_QTY INT, WEEK7_AMOUNT FLOAT,
 				       WEEK8_QTY INT, WEEK8_AMOUNT FLOAT, WEEK9_QTY INT, WEEK9_AMOUNT FLOAT,
 				       WEEK10_QTY INT, WEEK10_AMOUNT FLOAT, WEEK11_QTY INT, WEEK11_AMOUNT FLOAT,
 				       WEEK12_QTY INT, WEEK12_AMOUNT FLOAT, WEEK13_QTY INT, WEEK13_AMOUNT FLOAT,												
 				       WEEK14_QTY INT, WEEK14_AMOUNT FLOAT, CQTRPLUS1_QTY INT, CQTRPLUS1_AMOUNT FLOAT, 	
 				       CQTRPLUS2_QTY INT, CQTRPLUS2_AMOUNT FLOAT, CQTRPLUS3_QTY INT, CQTRPLUS3_AMOUNT FLOAT, 			 				 		
 				       WEEK_ID INT, TOTAL_WEEKS_QTY INT, TOTAL_WEEKS_AMOUNT FLOAT )

	INSERT INTO @RESULTS_TABLE ( BOOKING_WEEK, WEEK_ID ) VALUES ( 'CURRENT', 0 )
	INSERT INTO @RESULTS_TABLE ( BOOKING_WEEK, WEEK_ID ) VALUES ( 'WEEK1', 1 )
	INSERT INTO @RESULTS_TABLE ( BOOKING_WEEK, WEEK_ID ) VALUES ( 'WEEK2', 2 )
	INSERT INTO @RESULTS_TABLE ( BOOKING_WEEK, WEEK_ID ) VALUES ( 'WEEK3', 3 )
	INSERT INTO @RESULTS_TABLE ( BOOKING_WEEK, WEEK_ID ) VALUES ( 'WEEK4', 4 )
	INSERT INTO @RESULTS_TABLE ( BOOKING_WEEK, WEEK_ID ) VALUES ( 'WEEK5', 5 )
	INSERT INTO @RESULTS_TABLE ( BOOKING_WEEK, WEEK_ID ) VALUES ( 'WEEK6', 6 )
	INSERT INTO @RESULTS_TABLE ( BOOKING_WEEK, WEEK_ID ) VALUES ( 'WEEK7', 7 )
	INSERT INTO @RESULTS_TABLE ( BOOKING_WEEK, WEEK_ID ) VALUES ( 'WEEK8', 8 )
	INSERT INTO @RESULTS_TABLE ( BOOKING_WEEK, WEEK_ID ) VALUES ( 'WEEK9', 9 )
	INSERT INTO @RESULTS_TABLE ( BOOKING_WEEK, WEEK_ID ) VALUES ( 'WEEK10', 10 )
	INSERT INTO @RESULTS_TABLE ( BOOKING_WEEK, WEEK_ID ) VALUES ( 'WEEK11', 11 )
	INSERT INTO @RESULTS_TABLE ( BOOKING_WEEK, WEEK_ID ) VALUES ( 'WEEK12', 12 )
	INSERT INTO @RESULTS_TABLE ( BOOKING_WEEK, WEEK_ID ) VALUES ( 'WEEK13', 13 )
	INSERT INTO @RESULTS_TABLE ( BOOKING_WEEK, WEEK_ID ) VALUES ( 'WEEK14', 14 )

	IF ( @CURRENT_QTR = 1 ) 
	BEGIN
		SET @SYSTEM_WEEK = 1	
		SET @CURRENT_QTR_START = '01/01/' + CONVERT( VARCHAR, @CURRENT_YEAR )
		SET @CURRENT_QTR_END = '03/31/'+CONVERT(VARCHAR, @CURRENT_YEAR )
	END
	IF ( @CURRENT_QTR = 2 )
	BEGIN
		SET @SYSTEM_WEEK = 14		
		SET @CURRENT_QTR_START = '04/01/' + CONVERT( VARCHAR, @CURRENT_YEAR )
		SET @CURRENT_QTR_END = '06/30/'+CONVERT(VARCHAR, @CURRENT_YEAR )
	END 
	IF ( @CURRENT_QTR = 3 ) 
	BEGIN
		SET @SYSTEM_WEEK = 27
		SET @CURRENT_QTR_START = '07/01/' + CONVERT( VARCHAR, @CURRENT_YEAR )
		SET @CURRENT_QTR_END = '09/30/'+CONVERT(VARCHAR, @CURRENT_YEAR )
	END
	IF ( @CURRENT_QTR = 4 ) 
	BEGIN
		SET @SYSTEM_WEEK = 40
		SET @CURRENT_QTR_START = '10/01/' + CONVERT( VARCHAR, @CURRENT_YEAR )
		SET @CURRENT_QTR_END = '12/31/'+CONVERT(VARCHAR, @CURRENT_YEAR )
	END

	DECLARE @PRODUCT_CURSOR CURSOR
	DECLARE @LINE_ITEM_CURSOR CURSOR

	IF ( @HOLDTYPE = 'WITHHOLD' )
	BEGIN
		IF ( @BASEPRODUCTID = '0' )
		BEGIN
			SET @LINE_ITEM_CURSOR = CURSOR FOR
			SELECT LINEITEM_ID, Product_ID, ISNULL( COOL_QTY, 0 ), COOL_UNIT_COST, COOL_REQUESTED_DATE, 
			ISNULL( COOL_Revised_Date, ISNULL( COOL_SCHEDULE_DATE, GETDATE() ) ) AS COOL_SCHEDULE_DATE, SO_BOOKING_DATE, 0 AS IS_SHIPPED_QTY_REDUCE
			FROM CUSTOMER_OPEN_ORDER_LINEITEM COOL , SALES_ORDER SO 
			WHERE SO.SO_IS_SAMPLE = 1 AND COOL.SALESORDER_ID = SO.SALESORDER_ID AND COOL_STATUS <> 'SHIPPED'
			UNION ALL
			SELECT CSLIT.LINEITEM_ID, COOL.PRODUCT_ID, CSLIT.CSLIT_QTY_SHIPPED, COOL.COOL_UNIT_COST, COOL.COOL_REQUESTED_DATE, 
			ISNULL( COOL.COOL_REVISED_DATE, ISNULL( COOL.COOL_SCHEDULE_DATE, GETDATE() ) ) AS COOL_SCHEDULE_DATE, SO_BOOKING_DATE, 1 AS IS_SHIPPED_QTY_REDUCE
			FROM CUSTOMER_OPEN_ORDER_LINEITEM COOL, SALES_ORDER SO, CUSTOMER_SHIPPING_TABLE CST, CUSTOMER_SHIPPING_LINE_ITEM_TABLE CSLIT
			WHERE SO.SO_IS_SAMPLE = 1 AND COOL.SALESORDER_ID = SO.SALESORDER_ID AND CST.SALESORDER_ID = SO.SALESORDER_ID AND CST.CUSTOMERSHIPPING_ID = CSLIT.CUSTOMERSHIPPING_ID AND
			COOL.LINEITEM_ID = CSLIT.LINEITEM_ID AND CST_STATUS = 'SHIPPED' AND CST_SHIP_DATE >= @CURRENT_QTR_START AND CST_SHIP_DATE <= @CURRENT_QTR_END
		END
		ELSE
		BEGIN
			SET @LINE_ITEM_CURSOR = CURSOR FOR
			SELECT LINEITEM_ID, Product_ID, ISNULL( COOL_QTY, 0 ), COOL_UNIT_COST, COOL_REQUESTED_DATE, 
			ISNULL( COOL_Revised_Date, ISNULL( COOL_SCHEDULE_DATE, GETDATE() ) ) AS COOL_SCHEDULE_DATE, SO_BOOKING_DATE, 0 AS IS_SHIPPED_QTY_REDUCE
			FROM CUSTOMER_OPEN_ORDER_LINEITEM COOL , SALES_ORDER SO 
			WHERE SO.SO_IS_SAMPLE = 1 AND COOL.SALESORDER_ID = SO.SALESORDER_ID AND COOL_STATUS <> 'SHIPPED'
			AND PRODUCT_ID IN ( SELECT PRODUCT_ID FROM PRODUCT PRO , PROCESS_STEP PS
 				WHERE PRO.PROCESSSTEP_ID = PS.PROCESSSTEP_ID AND PS_TYPE_NAME = 'FINISHED GOODS' 
 				AND ( PRO.P_PARENT_PRODUCT_ID = @BASEPRODUCTID OR PRO.PRODUCT_ID = @BASEPRODUCTID )
 				AND PRO.P_PARENT_PRODUCT_ID IS NOT NULL )
			UNION ALL
			SELECT CSLIT.LINEITEM_ID, COOL.PRODUCT_ID, CSLIT.CSLIT_QTY_SHIPPED, COOL.COOL_UNIT_COST, COOL.COOL_REQUESTED_DATE, 
			ISNULL( COOL.COOL_REVISED_DATE, ISNULL( COOL.COOL_SCHEDULE_DATE, GETDATE() ) ) AS COOL_SCHEDULE_DATE, SO_BOOKING_DATE, 1 AS IS_SHIPPED_QTY_REDUCE
			FROM CUSTOMER_OPEN_ORDER_LINEITEM COOL, SALES_ORDER SO, CUSTOMER_SHIPPING_TABLE CST, CUSTOMER_SHIPPING_LINE_ITEM_TABLE CSLIT
			WHERE SO.SO_IS_SAMPLE = 1 AND COOL.SALESORDER_ID = SO.SALESORDER_ID AND CST.SALESORDER_ID = SO.SALESORDER_ID AND CST.CUSTOMERSHIPPING_ID = CSLIT.CUSTOMERSHIPPING_ID AND
			COOL.LINEITEM_ID = CSLIT.LINEITEM_ID AND CST_STATUS = 'SHIPPED' AND CST_SHIP_DATE >= @CURRENT_QTR_START AND CST_SHIP_DATE <= @CURRENT_QTR_END
			AND PRODUCT_ID IN ( SELECT PRODUCT_ID FROM PRODUCT PRO , PROCESS_STEP PS
 				WHERE PRO.PROCESSSTEP_ID = PS.PROCESSSTEP_ID AND PS_TYPE_NAME = 'FINISHED GOODS' 
 				AND ( PRO.P_PARENT_PRODUCT_ID = @BASEPRODUCTID OR PRO.PRODUCT_ID = @BASEPRODUCTID )
 				AND PRO.P_PARENT_PRODUCT_ID IS NOT NULL )

		END
	END
	ELSE
	BEGIN
		IF ( @BASEPRODUCTID = '0' )
		BEGIN
			SET @LINE_ITEM_CURSOR = CURSOR FOR
			SELECT LINEITEM_ID, Product_ID, ISNULL( COOL_QTY, 0 ), COOL_UNIT_COST, COOL_REQUESTED_DATE, 
			ISNULL( COOL_REVISED_DATE, ISNULL( COOL_SCHEDULE_DATE, GETDATE() ) ) AS COOL_SCHEDULE_DATE, SO_BOOKING_DATE, 0 AS IS_SHIPPED_QTY_REDUCE
			FROM CUSTOMER_OPEN_ORDER_LINEITEM COOL , SALES_ORDER SO 
			WHERE SO.SO_IS_SAMPLE = 1 AND COOL.SALESORDER_ID = SO.SALESORDER_ID AND COOL_STATUS = 'APPROVED'
			UNION ALL
			SELECT CSLIT.LINEITEM_ID, COOL.PRODUCT_ID, CSLIT.CSLIT_QTY_SHIPPED, COOL.COOL_UNIT_COST, COOL.COOL_REQUESTED_DATE, 
			ISNULL( COOL.COOL_REVISED_DATE, ISNULL( COOL.COOL_SCHEDULE_DATE, GETDATE() ) ) AS COOL_SCHEDULE_DATE, SO_BOOKING_DATE, 1 AS IS_SHIPPED_QTY_REDUCE
			FROM CUSTOMER_OPEN_ORDER_LINEITEM COOL, SALES_ORDER SO, CUSTOMER_SHIPPING_TABLE CST, CUSTOMER_SHIPPING_LINE_ITEM_TABLE CSLIT
			WHERE SO.SO_IS_SAMPLE = 1 AND COOL.SALESORDER_ID = SO.SALESORDER_ID AND CST.SALESORDER_ID = SO.SALESORDER_ID and CST.CUSTOMERSHIPPING_ID = CSLIT.CUSTOMERSHIPPING_ID AND
			COOL.LINEITEM_ID = CSLIT.LINEITEM_ID AND CST_STATUS = 'SHIPPED' AND CST_SHIP_DATE >= @CURRENT_QTR_START AND CST_SHIP_DATE <= @CURRENT_QTR_END
		END
		ELSE
		BEGIN
			SET @LINE_ITEM_CURSOR = CURSOR FOR
			SELECT LINEITEM_ID, Product_ID, ISNULL( COOL_QTY, 0 ), COOL_UNIT_COST, COOL_REQUESTED_DATE, 
			ISNULL( COOL_REVISED_DATE, ISNULL( COOL_SCHEDULE_DATE, GETDATE() ) ) AS COOL_SCHEDULE_DATE, SO_BOOKING_DATE, 0 AS IS_SHIPPED_QTY_REDUCE
			FROM CUSTOMER_OPEN_ORDER_LINEITEM COOL , SALES_ORDER SO 
			WHERE SO.SO_IS_SAMPLE = 1 AND COOL.SALESORDER_ID = SO.SALESORDER_ID AND COOL_STATUS = 'APPROVED'
			AND PRODUCT_ID IN ( SELECT PRODUCT_ID FROM PRODUCT PRO , PROCESS_STEP PS
 				WHERE PRO.PROCESSSTEP_ID = PS.PROCESSSTEP_ID AND PS_TYPE_NAME = 'FINISHED GOODS' 
 				AND ( PRO.P_PARENT_PRODUCT_ID = @BASEPRODUCTID OR PRO.PRODUCT_ID = @BASEPRODUCTID )
 				AND PRO.P_PARENT_PRODUCT_ID IS NOT NULL )
			UNION ALL
			SELECT CSLIT.LINEITEM_ID, COOL.PRODUCT_ID, CSLIT.CSLIT_QTY_SHIPPED, COOL.COOL_UNIT_COST, COOL.COOL_REQUESTED_DATE, 
			ISNULL( COOL.COOL_REVISED_DATE, ISNULL( COOL.COOL_SCHEDULE_DATE, GETDATE() ) ) AS COOL_SCHEDULE_DATE, SO_BOOKING_DATE, 1 AS IS_SHIPPED_QTY_REDUCE
			FROM CUSTOMER_OPEN_ORDER_LINEITEM COOL, SALES_ORDER SO, CUSTOMER_SHIPPING_TABLE CST, CUSTOMER_SHIPPING_LINE_ITEM_TABLE CSLIT
			WHERE SO.SO_IS_SAMPLE = 1 AND COOL.SALESORDER_ID = SO.SALESORDER_ID AND CST.SALESORDER_ID = SO.SALESORDER_ID AND CST.CUSTOMERSHIPPING_ID = CSLIT.CUSTOMERSHIPPING_ID AND
			COOL.LINEITEM_ID = CSLIT.LINEITEM_ID AND CST_STATUS = 'SHIPPED' AND CST_SHIP_DATE >= @CURRENT_QTR_START AND CST_SHIP_DATE <= @CURRENT_QTR_END
			AND PRODUCT_ID IN ( SELECT PRODUCT_ID FROM PRODUCT PRO , PROCESS_STEP PS
 				WHERE PRO.PROCESSSTEP_ID = PS.PROCESSSTEP_ID AND PS_TYPE_NAME = 'FINISHED GOODS' 
 				AND ( PRO.P_PARENT_PRODUCT_ID = @BASEPRODUCTID OR PRO.PRODUCT_ID = @BASEPRODUCTID )
 				AND PRO.P_PARENT_PRODUCT_ID IS NOT NULL )
		END
	END

	OPEN @LINE_ITEM_CURSOR
	FETCH NEXT FROM @LINE_ITEM_CURSOR 
	INTO @LINEITEM_ID, @PRODUCT_ID, @COOL_QTY, @COOL_AMOUNT, @COOL_REQUESTED_DATE, @COOL_SCHEDULE_DATE, @SO_BOOKING_DATE, @IS_SHIPPED_QTY_REDUCE

	WHILE @@FETCH_STATUS = 0
	BEGIN
		IF ( @CATEGORY = 'REQUESTED' )
		BEGIN
			SET @CATEGORY_DATE = @COOL_REQUESTED_DATE
		END
		ELSE
		BEGIN
			SET @CATEGORY_DATE = @COOL_SCHEDULE_DATE
		END

		IF ( @SO_BOOKING_DATE > DATEADD( mm, 3, @CURRENT_QTR_START ) )
		BEGIN
			FETCH NEXT FROM @LINE_ITEM_CURSOR 
			INTO @LINEITEM_ID, @PRODUCT_ID, @COOL_QTY, @COOL_AMOUNT, @COOL_REQUESTED_DATE, @COOL_SCHEDULE_DATE, @SO_BOOKING_DATE, @IS_SHIPPED_QTY_REDUCE		
			CONTINUE
		END 

		SET @LINEITEM_QTR  = DATEPART( QQ, @CATEGORY_DATE )
		SET @LINEITEM_YEAR = DATEPART( YYYY, @CATEGORY_DATE )

		SET @BOOKING_DATE_QTR 	= DATEPART( QQ, @SO_BOOKING_DATE )
		SET @BOOKING_DATE_YEAR 	= DATEPART( YYYY, @SO_BOOKING_DATE ) 

		IF( @IS_SHIPPED_QTY_REDUCE = 0 )
		BEGIN
			SELECT @PARTIAL_SHIPPED_QTY = ISNULL( SUM( CSLIT_QTY_SHIPPED ), 0 ) FROM CUSTOMER_SHIPPING_LINE_ITEM_TABLE CSLIT, CUSTOMER_SHIPPING_TABLE CST 
			WHERE CSLIT.CUSTOMERSHIPPING_ID = CST.CUSTOMERSHIPPING_ID AND LINEITEM_ID = @LINEITEM_ID AND CSLIT_STATUS = 'SHIPPED' --AND CST_SHIP_DATE < @CURRENT_QTR_START
			AND CST_SHIP_DATE <= @CURRENT_QTR_END
		END
		ELSE
		BEGIN
			SET @PARTIAL_SHIPPED_QTY = 0
		END

		IF ( @COOL_QTY - @PARTIAL_SHIPPED_QTY > 0 )
		BEGIN
			SET @COOL_QTY = @COOL_QTY - @PARTIAL_SHIPPED_QTY

			-- THIS CONDITION IS TO HANDLE ALL THE BOOKINGS BEFORE THIS QUARTER START AND CATEGORY QUARTER IS THE PRESENT QUARTER
			IF( @SO_BOOKING_DATE < @CURRENT_QTR_START AND @LINEITEM_QTR = @CURRENT_QTR AND @LINEITEM_YEAR = @CURRENT_YEAR )
			BEGIN
				IF( @CATEGORY_DATE < @CURRENT_QTR_START )
				BEGIN
					SET @LINEITEM_WEEK = @SYSTEM_WEEK
				END
				ELSE
				BEGIN
					SET @LINEITEM_WEEK = DATEPART( WW, @CATEGORY_DATE )
				END

				IF ( @LINEITEM_WEEK = @SYSTEM_WEEK ) 
				BEGIN
					UPDATE @RESULTS_TABLE
					SET WEEK1_QTY = ISNULL(WEEK1_QTY,0) + @COOL_QTY , WEEK1_AMOUNT = ISNULL(WEEK1_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = 'CURRENT'
				END
				IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 1) 
				BEGIN
					UPDATE @RESULTS_TABLE
					SET WEEK2_QTY = ISNULL(WEEK2_QTY,0) + @COOL_QTY , WEEK2_AMOUNT = ISNULL(WEEK2_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = 'CURRENT'
				END
				IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 2) 
				BEGIN
					UPDATE @RESULTS_TABLE
					SET WEEK3_QTY = ISNULL(WEEK3_QTY,0) + @COOL_QTY , WEEK3_AMOUNT = ISNULL(WEEK3_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = 'CURRENT'	
				END
				IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 3) 
				BEGIN
					UPDATE @RESULTS_TABLE
					SET WEEK4_QTY = ISNULL(WEEK4_QTY,0) + @COOL_QTY , WEEK4_AMOUNT = ISNULL(WEEK4_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = 'CURRENT'
				END
				IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 4) 
				BEGIN
					UPDATE @RESULTS_TABLE
					SET WEEK5_QTY = ISNULL(WEEK5_QTY,0) + @COOL_QTY , WEEK5_AMOUNT = ISNULL(WEEK5_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = 'CURRENT'
				END
				IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 5) 
				BEGIN
					UPDATE @RESULTS_TABLE
					SET WEEK6_QTY = ISNULL(WEEK6_QTY,0) + @COOL_QTY , WEEK6_AMOUNT = ISNULL(WEEK6_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = 'CURRENT'
				END
				IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 6) 
				BEGIN
					UPDATE @RESULTS_TABLE
					SET WEEK7_QTY = ISNULL(WEEK7_QTY,0) + @COOL_QTY , WEEK7_AMOUNT = ISNULL(WEEK7_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = 'CURRENT'
				END
				IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 7) 
				BEGIN
					UPDATE @RESULTS_TABLE
					SET WEEK8_QTY = ISNULL(WEEK8_QTY,0) + @COOL_QTY , WEEK8_AMOUNT = ISNULL(WEEK8_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = 'CURRENT'
				END
				IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 8) 
				BEGIN
					UPDATE @RESULTS_TABLE
					SET WEEK9_QTY = ISNULL(WEEK9_QTY,0) + @COOL_QTY , WEEK9_AMOUNT = ISNULL(WEEK9_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = 'CURRENT'
				END
				IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 9) 
				BEGIN
					UPDATE @RESULTS_TABLE
					SET WEEK10_QTY = ISNULL(WEEK10_QTY,0) + @COOL_QTY , WEEK10_AMOUNT = ISNULL(WEEK10_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = 'CURRENT'
				END
				IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 10) 
				BEGIN
					UPDATE @RESULTS_TABLE
					SET WEEK11_QTY = ISNULL(WEEK11_QTY,0) + @COOL_QTY , WEEK11_AMOUNT = ISNULL(WEEK11_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = 'CURRENT'
				END
				IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 11) 
				BEGIN
					UPDATE @RESULTS_TABLE
					SET WEEK12_QTY = ISNULL(WEEK12_QTY,0) + @COOL_QTY , WEEK12_AMOUNT = ISNULL(WEEK12_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = 'CURRENT'
				END
				IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 12) 
				BEGIN
					UPDATE @RESULTS_TABLE
					SET WEEK13_QTY = ISNULL(WEEK13_QTY,0) + @COOL_QTY , WEEK13_AMOUNT = ISNULL(WEEK13_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = 'CURRENT'
				END
				IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 13) 
				BEGIN
					UPDATE @RESULTS_TABLE
					SET WEEK14_QTY = ISNULL(WEEK14_QTY,0) + @COOL_QTY , WEEK14_AMOUNT = ISNULL(WEEK14_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = 'CURRENT'
				END
			END
			-- THIS CONDITION IS TO HANDLE ALL THE BOOKINGS BEFORE THIS QUARTER START AND CATEGORY QUARTER AFTER THE PRESENT QUARTER
			ELSE IF( @SO_BOOKING_DATE < @CURRENT_QTR_START AND ( ( @LINEITEM_QTR > @CURRENT_QTR ) OR ( @LINEITEM_YEAR = @CURRENT_YEAR + 1 AND @LINEITEM_QTR < @CURRENT_QTR ) ) )
			BEGIN
				IF ( @LINEITEM_QTR = @CURRENT_QTR + 1 AND @LINEITEM_YEAR = @CURRENT_YEAR )
				BEGIN
					UPDATE @RESULTS_TABLE
					SET CQTRPLUS1_QTY = ISNULL(CQTRPLUS1_QTY,0) + @COOL_QTY , CQTRPLUS1_AMOUNT = ISNULL(CQTRPLUS1_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = 'CURRENT'
				END
				ELSE IF ( @LINEITEM_QTR = @CURRENT_QTR + 2 AND @LINEITEM_YEAR = @CURRENT_YEAR )
				BEGIN
					UPDATE @RESULTS_TABLE
					SET CQTRPLUS2_QTY = ISNULL(CQTRPLUS2_QTY,0) + @COOL_QTY , CQTRPLUS2_AMOUNT = ISNULL(CQTRPLUS2_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = 'CURRENT'
				END
				ELSE IF ( @LINEITEM_QTR = @CURRENT_QTR + 3 AND @LINEITEM_YEAR = @CURRENT_YEAR )
				BEGIN
					UPDATE @RESULTS_TABLE
					SET CQTRPLUS3_QTY = ISNULL(CQTRPLUS3_QTY,0) + @COOL_QTY , CQTRPLUS3_AMOUNT = ISNULL(CQTRPLUS3_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = 'CURRENT'
				END
				ELSE IF ( ( @LINEITEM_QTR + 5 - @CURRENT_QTR ) = 2 AND @LINEITEM_YEAR = @CURRENT_YEAR + 1 )
				BEGIN
					UPDATE @RESULTS_TABLE
					SET CQTRPLUS1_QTY = ISNULL(CQTRPLUS1_QTY,0) + @COOL_QTY , CQTRPLUS1_AMOUNT = ISNULL(CQTRPLUS1_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = 'CURRENT'
 				END
				ELSE IF ( ( @LINEITEM_QTR + 5 - @CURRENT_QTR ) = 3 AND @LINEITEM_YEAR = @CURRENT_YEAR + 1 )
				BEGIN
					UPDATE @RESULTS_TABLE
					SET CQTRPLUS2_QTY = ISNULL(CQTRPLUS2_QTY,0) + @COOL_QTY , CQTRPLUS2_AMOUNT = ISNULL(CQTRPLUS2_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = 'CURRENT'
				END
 				ELSE IF ( ( @LINEITEM_QTR + 5 - @CURRENT_QTR ) = 4 AND @LINEITEM_YEAR = @CURRENT_YEAR + 1 )
				BEGIN
 					UPDATE @RESULTS_TABLE
 					SET CQTRPLUS3_QTY = ISNULL(CQTRPLUS3_QTY,0) + @COOL_QTY , CQTRPLUS3_AMOUNT = ISNULL(CQTRPLUS3_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
 					WHERE BOOKING_WEEK = 'CURRENT'
 				END
			END
			-- THIS CONDITION HANDLES ALL THE BOOKINGS IN THIS QUARTER AND IF THE CATEGORY DATE IS ALSO IN THIS QUARTER
			ELSE IF ( @BOOKING_DATE_QTR = @CURRENT_QTR AND @LINEITEM_YEAR = @CURRENT_YEAR AND @LINEITEM_QTR = @CURRENT_QTR )
			BEGIN
				SET @LINEITEM_WEEK = DATEPART( WW, @CATEGORY_DATE )
				SET @BOOKING_DATE_WEEK = DATEPART( WW, @SO_BOOKING_DATE )
				SET @BOOKINGWEEK = 'WEEK' + CONVERT( VARCHAR, ( @BOOKING_DATE_WEEK - @SYSTEM_WEEK  + 1 ) )

				IF ( @LINEITEM_WEEK = @SYSTEM_WEEK ) 
				BEGIN
					UPDATE @RESULTS_TABLE
					SET WEEK1_QTY = ISNULL(WEEK1_QTY,0) + @COOL_QTY , WEEK1_AMOUNT = ISNULL(WEEK1_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = @BOOKINGWEEK
				END
				IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 1) 
				BEGIN
					UPDATE @RESULTS_TABLE
					SET WEEK2_QTY = ISNULL(WEEK2_QTY,0) + @COOL_QTY , WEEK2_AMOUNT = ISNULL(WEEK2_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = @BOOKINGWEEK
				END
				IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 2) 
				BEGIN
					UPDATE @RESULTS_TABLE
					SET WEEK3_QTY = ISNULL(WEEK3_QTY,0) + @COOL_QTY , WEEK3_AMOUNT = ISNULL(WEEK3_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = @BOOKINGWEEK
				END
				IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 3) 
				BEGIN
					UPDATE @RESULTS_TABLE
					SET WEEK4_QTY = ISNULL(WEEK4_QTY,0) + @COOL_QTY , WEEK4_AMOUNT = ISNULL(WEEK4_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = @BOOKINGWEEK
				END
				IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 4) 
				BEGIN
					UPDATE @RESULTS_TABLE
					SET WEEK5_QTY = ISNULL(WEEK5_QTY,0) + @COOL_QTY , WEEK5_AMOUNT = ISNULL(WEEK5_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = @BOOKINGWEEK
				END
				IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 5) 
				BEGIN
					UPDATE @RESULTS_TABLE
					SET WEEK6_QTY = ISNULL(WEEK6_QTY,0) + @COOL_QTY , WEEK6_AMOUNT = ISNULL(WEEK6_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = @BOOKINGWEEK
				END
				IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 6) 
				BEGIN
					UPDATE @RESULTS_TABLE
					SET WEEK7_QTY = ISNULL(WEEK7_QTY,0) + @COOL_QTY , WEEK7_AMOUNT = ISNULL(WEEK7_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = @BOOKINGWEEK
				END
				IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 7) 
				BEGIN
					UPDATE @RESULTS_TABLE
					SET WEEK8_QTY = ISNULL(WEEK8_QTY,0) + @COOL_QTY , WEEK8_AMOUNT = ISNULL(WEEK8_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = @BOOKINGWEEK
				END
				IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 8) 
				BEGIN
					UPDATE @RESULTS_TABLE
					SET WEEK9_QTY = ISNULL(WEEK9_QTY,0) + @COOL_QTY , WEEK9_AMOUNT = ISNULL(WEEK9_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = @BOOKINGWEEK
				END
				IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 9) 
				BEGIN
					UPDATE @RESULTS_TABLE
					SET WEEK10_QTY = ISNULL(WEEK10_QTY,0) + @COOL_QTY , WEEK10_AMOUNT = ISNULL(WEEK10_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = @BOOKINGWEEK
				END
				IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 10) 
				BEGIN
					UPDATE @RESULTS_TABLE
					SET WEEK11_QTY = ISNULL(WEEK11_QTY,0) + @COOL_QTY , WEEK11_AMOUNT = ISNULL(WEEK11_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = @BOOKINGWEEK
				END
				IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 11) 
				BEGIN
					UPDATE @RESULTS_TABLE
					SET WEEK12_QTY = ISNULL(WEEK12_QTY,0) + @COOL_QTY , WEEK12_AMOUNT = ISNULL(WEEK12_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = @BOOKINGWEEK
				END
				IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 12) 
				BEGIN
					UPDATE @RESULTS_TABLE
					SET WEEK13_QTY = ISNULL(WEEK13_QTY,0) + @COOL_QTY , WEEK13_AMOUNT = ISNULL(WEEK13_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = @BOOKINGWEEK
				END
				IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 13) 
				BEGIN
					UPDATE @RESULTS_TABLE
					SET WEEK14_QTY = ISNULL(WEEK14_QTY,0) + @COOL_QTY , WEEK14_AMOUNT = ISNULL(WEEK14_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = @BOOKINGWEEK
				END
			END
			-- THIS CONDITION HANDLES ALL THE BOOKINGS IN THIS QUARTER AND IF THE CATEGORY DATE IS NOT IN THIS QUARTER
			ELSE IF ( @BOOKING_DATE_QTR = @CURRENT_QTR AND ( ( @LINEITEM_YEAR = @CURRENT_YEAR AND @LINEITEM_QTR > @CURRENT_QTR ) OR ( @LINEITEM_YEAR = @CURRENT_YEAR + 1 AND @LINEITEM_QTR < @CURRENT_QTR ) ) )
			BEGIN
				SET @BOOKING_DATE_WEEK = DATEPART( WW, @SO_BOOKING_DATE )
				SET @BOOKINGWEEK = 'WEEK' + CONVERT( VARCHAR, ( @BOOKING_DATE_WEEK - @SYSTEM_WEEK + 1 ) )

				IF ( @LINEITEM_QTR = @CURRENT_QTR + 1 AND @LINEITEM_YEAR = @CURRENT_YEAR )
				BEGIN
					UPDATE @RESULTS_TABLE
					SET CQTRPLUS1_QTY = ISNULL(CQTRPLUS1_QTY,0) + @COOL_QTY , CQTRPLUS1_AMOUNT = ISNULL(CQTRPLUS1_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = @BOOKINGWEEK
				END
				ELSE IF ( @LINEITEM_QTR = @CURRENT_QTR + 2 AND @LINEITEM_YEAR = @CURRENT_YEAR )
				BEGIN
					UPDATE @RESULTS_TABLE
					SET CQTRPLUS2_QTY = ISNULL(CQTRPLUS2_QTY,0) + @COOL_QTY , CQTRPLUS2_AMOUNT = ISNULL(CQTRPLUS2_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = @BOOKINGWEEK
				END
				ELSE IF ( @LINEITEM_QTR = @CURRENT_QTR + 3 AND @LINEITEM_YEAR = @CURRENT_YEAR )
				BEGIN
					UPDATE @RESULTS_TABLE
					SET CQTRPLUS3_QTY = ISNULL(CQTRPLUS3_QTY,0) + @COOL_QTY , CQTRPLUS3_AMOUNT = ISNULL(CQTRPLUS3_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = @BOOKINGWEEK
				END
				ELSE IF ( ( @LINEITEM_QTR + 5 - @CURRENT_QTR ) = 2 AND @LINEITEM_YEAR = @CURRENT_YEAR + 1 )
				BEGIN
					UPDATE @RESULTS_TABLE
					SET CQTRPLUS1_QTY = ISNULL(CQTRPLUS1_QTY,0) + @COOL_QTY , CQTRPLUS1_AMOUNT = ISNULL(CQTRPLUS1_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = @BOOKINGWEEK
 				END
				ELSE IF ( ( @LINEITEM_QTR + 5 - @CURRENT_QTR ) = 3 AND @LINEITEM_YEAR = @CURRENT_YEAR + 1 )
				BEGIN
					UPDATE @RESULTS_TABLE
					SET CQTRPLUS2_QTY = ISNULL(CQTRPLUS2_QTY,0) + @COOL_QTY , CQTRPLUS2_AMOUNT = ISNULL(CQTRPLUS2_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE BOOKING_WEEK = @BOOKINGWEEK
				END
 				ELSE IF ( ( @LINEITEM_QTR + 5 - @CURRENT_QTR ) = 4 AND @LINEITEM_YEAR = @CURRENT_YEAR + 1 )
				BEGIN
 					UPDATE @RESULTS_TABLE
 					SET CQTRPLUS3_QTY = ISNULL(CQTRPLUS3_QTY,0) + @COOL_QTY , CQTRPLUS3_AMOUNT = ISNULL(CQTRPLUS3_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
 					WHERE BOOKING_WEEK = @BOOKINGWEEK
 				END
			END
			ELSE IF ( @LINEITEM_QTR < @CURRENT_QTR AND @LINEITEM_YEAR = @CURRENT_YEAR )
			BEGIN
				UPDATE @RESULTS_TABLE
				SET DELIQUENT_QTY = ISNULL( DELIQUENT_QTY, 0 ) + @COOL_QTY, DELIQUENT_AMOUNT = ISNULL( DELIQUENT_AMOUNT, 0 ) + ( @COOL_QTY * @COOL_AMOUNT )
				WHERE BOOKING_WEEK = 'CURRENT'
			END
			ELSE IF( @LINEITEM_YEAR < @CURRENT_YEAR )
			BEGIN
				UPDATE @RESULTS_TABLE
				SET DELIQUENT_QTY = ISNULL( DELIQUENT_QTY, 0 ) + @COOL_QTY, DELIQUENT_AMOUNT = ISNULL( DELIQUENT_AMOUNT, 0 ) + ( @COOL_QTY * @COOL_AMOUNT )
				WHERE BOOKING_WEEK = 'CURRENT'
			END
		END-- if cool-qty > 0 ends here
		SET @COOL_QTY = NULL
		SET @COOL_AMOUNT = NULL
		SET @COOL_REQUESTED_DATE = NULL
		SET @COOL_SCHEDULE_DATE	= NULL
		SET @IS_SHIPPED_QTY_REDUCE = NULL

 		FETCH NEXT FROM @LINE_ITEM_CURSOR 
		INTO @LINEITEM_ID, @PRODUCT_ID, @COOL_QTY, @COOL_AMOUNT, @COOL_REQUESTED_DATE, @COOL_SCHEDULE_DATE, @SO_BOOKING_DATE, @IS_SHIPPED_QTY_REDUCE	
	END -- while loop ends here
	CLOSE @LINE_ITEM_CURSOR
	DEALLOCATE @LINE_ITEM_CURSOR	 


	DECLARE @CUMULATIVE_QTY INT 	
	DECLARE @BOOKING_WEEK_ID INT
	DECLARE @TOTAL_WEEKS_QTY INT
	DECLARE @TOTAL_WEEKS_AMOUNT FLOAT
	DECLARE @TEMP_TOTAL_WEEKS_QTY INT
	DECLARE @TEMP_TOTAL_WEEKS_AMOUNT FLOAT	
	DECLARE @CQTRPLUS1_QTY INT
	DECLARE @CQTRPLUS1_AMOUNT FLOAT
	DECLARE @CQTRPLUS2_QTY INT
	DECLARE @CQTRPLUS2_AMOUNT FLOAT
	DECLARE @CQTRPLUS3_QTY INT
	DECLARE @CQTRPLUS3_AMOUNT FLOAT

	SET @TEMP_TOTAL_WEEKS_QTY = 0
	SET @TEMP_TOTAL_WEEKS_AMOUNT = 0	
	SET @TOTAL_WEEKS_QTY = 0
	SET @TOTAL_WEEKS_AMOUNT = 0	
	SET @CQTRPLUS1_QTY = 0
	SET @CQTRPLUS1_AMOUNT = 0
	SET @CQTRPLUS2_QTY = 0
	SET @CQTRPLUS2_AMOUNT = 0
	SET @CQTRPLUS3_QTY = 0
	SET @CQTRPLUS3_AMOUNT = 0
	SET @CUMULATIVE_QTY = 0

	SET @PRODUCT_CURSOR = CURSOR FOR
	SELECT WEEK_ID FROM @RESULTS_TABLE ORDER BY WEEK_ID 

	OPEN @PRODUCT_CURSOR
	FETCH NEXT FROM @PRODUCT_CURSOR INTO @BOOKING_WEEK_ID

	WHILE @@FETCH_STATUS = 0
	BEGIN
		SET @LINE_ITEM_CURSOR = CURSOR FOR SELECT TOTAL_WEEKS_QTY, TOTAL_WEEKS_AMOUNT, CQTRPLUS1_QTY, CQTRPLUS1_AMOUNT, CQTRPLUS2_QTY, CQTRPLUS2_AMOUNT, CQTRPLUS3_QTY, CQTRPLUS3_AMOUNT
		FROM @RESULTS_TABLE WHERE WEEK_ID = @BOOKING_WEEK_ID

		OPEN @LINE_ITEM_CURSOR 
		FETCH NEXT FROM @LINE_ITEM_CURSOR  INTO @TOTAL_WEEKS_QTY, @TOTAL_WEEKS_AMOUNT, @CQTRPLUS1_QTY, @CQTRPLUS1_AMOUNT, @CQTRPLUS2_QTY, @CQTRPLUS2_AMOUNT, @CQTRPLUS3_QTY, @CQTRPLUS3_AMOUNT  
		WHILE @@FETCH_STATUS = 0
		BEGIN	
			UPDATE @RESULTS_TABLE SET 
				TOTAL_WEEKS_QTY	= ISNULL(TOTAL_WEEKS_QTY,0) + ISNULL(DELIQUENT_QTY,0) + ISNULL(WEEK1_QTY,0) + ISNULL(WEEK2_QTY,0) + ISNULL(WEEK3_QTY,0) + ISNULL(WEEK4_QTY,0) + ISNULL(WEEK5_QTY,0) + ISNULL(WEEK6_QTY,0) + ISNULL(WEEK7_QTY,0) + ISNULL(WEEK8_QTY,0) + ISNULL(WEEK9_QTY,0) + ISNULL(WEEK10_QTY,0) + ISNULL(WEEK11_QTY,0) + ISNULL(WEEK12_QTY,0) + ISNULL(WEEK13_QTY,0) + ISNULL(WEEK14_QTY,0),		
				TOTAL_WEEKS_AMOUNT = ISNULL(TOTAL_WEEKS_AMOUNT,0) + ISNULL(DELIQUENT_AMOUNT,0) + ISNULL(WEEK1_AMOUNT,0) + ISNULL(WEEK2_AMOUNT,0) + ISNULL(WEEK3_AMOUNT,0) + ISNULL(WEEK4_AMOUNT,0) + ISNULL(WEEK5_AMOUNT,0) + ISNULL(WEEK6_AMOUNT,0) + ISNULL(WEEK7_AMOUNT,0) + ISNULL(WEEK8_AMOUNT,0) + ISNULL(WEEK9_AMOUNT,0) + ISNULL(WEEK10_AMOUNT,0) + ISNULL(WEEK11_AMOUNT,0) + ISNULL(WEEK12_AMOUNT,0) + ISNULL(WEEK13_AMOUNT,0)+ ISNULL(WEEK14_AMOUNT,0)
			WHERE WEEK_ID = @BOOKING_WEEK_ID

			SELECT 	@TEMP_TOTAL_WEEKS_QTY = TOTAL_WEEKS_QTY,
				@TEMP_TOTAL_WEEKS_AMOUNT = TOTAL_WEEKS_AMOUNT 
			FROM @RESULTS_TABLE WHERE WEEK_ID = @BOOKING_WEEK_ID
	
			UPDATE @RESULTS_TABLE SET 
				TOTAL_WEEKS_QTY 	= ISNULL(TOTAL_WEEKS_QTY,0) + ISNULL(@TEMP_TOTAL_WEEKS_QTY,0), 
				TOTAL_WEEKS_AMOUNT 	= ISNULL(TOTAL_WEEKS_AMOUNT,0) + ISNULL(@TEMP_TOTAL_WEEKS_AMOUNT,0),
				CQTRPLUS1_QTY 		= ISNULL( CQTRPLUS1_QTY, 0) + ISNULL( @CQTRPLUS1_QTY, 0),
				CQTRPLUS1_AMOUNT 	= ISNULL( CQTRPLUS1_AMOUNT, 0) + ISNULL( @CQTRPLUS1_AMOUNT, 0),
				CQTRPLUS2_QTY 		= ISNULL( CQTRPLUS2_QTY, 0) + ISNULL( @CQTRPLUS2_QTY, 0),
				CQTRPLUS2_AMOUNT 	= ISNULL( CQTRPLUS2_AMOUNT, 0) + ISNULL( @CQTRPLUS2_AMOUNT, 0),
				CQTRPLUS3_QTY 		= ISNULL( CQTRPLUS3_QTY, 0) + ISNULL( @CQTRPLUS3_QTY, 0),
				CQTRPLUS3_AMOUNT 	= ISNULL( CQTRPLUS3_AMOUNT, 0) + ISNULL( @CQTRPLUS3_AMOUNT, 0)
			WHERE WEEK_ID = (@BOOKING_WEEK_ID + 1)
			SET @TOTAL_WEEKS_QTY = 0
			SET @TOTAL_WEEKS_AMOUNT = 0	
			SET @TEMP_TOTAL_WEEKS_QTY = 0
			SET @TEMP_TOTAL_WEEKS_AMOUNT = 0
			SET @CQTRPLUS1_QTY = 0
			SET @CQTRPLUS1_AMOUNT = 0
			SET @CQTRPLUS2_QTY = 0
			SET @CQTRPLUS2_AMOUNT = 0
			SET @CQTRPLUS3_QTY = 0
			SET @CQTRPLUS3_AMOUNT = 0
			

			FETCH NEXT FROM @LINE_ITEM_CURSOR INTO @TOTAL_WEEKS_QTY, @TOTAL_WEEKS_AMOUNT, @CQTRPLUS1_QTY, @CQTRPLUS1_AMOUNT, @CQTRPLUS2_QTY, @CQTRPLUS2_AMOUNT, @CQTRPLUS3_QTY, @CQTRPLUS3_AMOUNT  
		END
		CLOSE @LINE_ITEM_CURSOR
		DEALLOCATE @LINE_ITEM_CURSOR

		SET @BOOKING_WEEK_ID = 0
		
		FETCH NEXT FROM @PRODUCT_CURSOR INTO @BOOKING_WEEK_ID
	END
	CLOSE @PRODUCT_CURSOR
 	DEALLOCATE @PRODUCT_CURSOR


	SELECT * FROM @RESULTS_TABLE
 END
